package com.checker.springcloud.controller;

import com.checker.springcloud.entity.CommonResult;
import com.checker.springcloud.entity.Payment;
import com.checker.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "/payment", method = RequestMethod.POST)
    public CommonResult insertPayment(@RequestBody Payment payment) {
        int result = paymentService.insertPaument(payment);

        log.info("结果：" + result);

        if (result > 0) {
            return new CommonResult(200, "插入数据库成功: " + port, result);
        } else {
            return new CommonResult(444, "插入数据库失败");
        }
    }

    @RequestMapping(value = "/payment/{id}",method = RequestMethod.GET)
    public CommonResult getPaymentById(@PathVariable("id")Long id){
        Payment payment = paymentService.getPaymentById(id);

        log.info("结果：" +payment);

        if (payment != null){
            return new CommonResult(200, "获取数据成功: " + port, payment);
        } else {
            return new CommonResult(444, "获取数据失败");
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLB(){
        return port;
    }
}
